<?php
/**
 * Created by PhpStorm.
 * User: chongyi
 * Date: 2015/4/8
 * Time: 16:43
 */

namespace Test;

class ConvertGrowRecord extends Convert
{
    public function convert($school, $schoolid, $groupinfo, $spaceid, $classid, $class)
    {
        printf("  Grow record convert begin: \n");
        $all = $this->connection->table('yuanbao_v1.t_picture')->where('kindergartenId', '=', $school->id)
            ->where('classId', '=', $class->id)
            ->get();

        foreach ($all as $record) {

            $userid = $this->connection->table('convert.v2_sys_user')->where('user', '=', $record->publisherCode)->pluck('id');
            if ($userid === null) continue;
            $insert = array(
                'space' => $spaceid,
                'school_id' => $schoolid,
                'class_id' => $classid,
                'user_id' => $userid,
                'content' => $record->note ?: ($record->title ?: ''),
                'video' => $record->videoUrl ?: '',
                'created_time' => strtotime($record->publishTime),
                'tags' => str_replace(['0', '1', '2', '3', '4', '5'], ['其他', '健康', '学习', '科学', '艺术', '社会'], $record->type),
                'status' => 1
            );

            $id = $this->connection->table('convert.v2_school_student_grow')->insertGetId($insert);

            $pics = explode(',', $record->url);
            foreach ($pics as $pic)
            {
                printf("  * Insert picture: %s\n", $pic);
                $this->connection->table('convert.v2_school_student_grow_pic')->insert(array('parent_id' => $id, 'pic_url' => $pic));
            }
            // 转换评论
            (new ConvertGrowRecordComment($this->connection))->convert($record, $id);
        }
    }
} 